package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.Back_New_Adapter;
import com.zprmb.udzt.bean.Back_New_Uplan;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.VolleryUtil;

public class Back_money_new extends BaseActivity implements OnRefreshListener2<ListView>,OnClickListener{
	private PullToRefreshListView lv;
	private String investId;
	private RelativeLayout rl_back;
	private Back_New_Adapter adapter;
	
	private List<Back_New_Uplan> list=new ArrayList<Back_New_Uplan>();
	private List<Back_New_Uplan> list2=new ArrayList<Back_New_Uplan>();
	
	
	private ListView actualListView;
	private ImageView img_none;
	private int type;
	int page;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.back_money);
		investId=getIntent().getStringExtra("investId");
		lv = (PullToRefreshListView)findViewById(R.id.lv);
		img_none=(ImageView)findViewById(R.id.iv_v4_nonetwork);
		
		actualListView = lv.getRefreshableView();
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		type=getIntent().getIntExtra("type", 1);
		
		rl_back.setOnClickListener(this);
		lv.setOnRefreshListener(this);
		registerForContextMenu(actualListView);
		lv.setOnRefreshListener(this);
		lv.setMode(Mode.BOTH);
		initData();
	}
	private void initData() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("investId", investId);
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e("回款计划", jsonObject.toString());
				Gson gson=new Gson();
				if (!jsonObject.optString("data").equals("null")) {
					list=gson.fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<Back_New_Uplan>>() {
						}.getType());
				
				}
				list2.addAll(list);			
				
					adapter=new Back_New_Adapter(list2, Back_money_new.this,getIntent().getStringExtra("title"));
					lv.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				
			
				actualListView.setSelection(list2.size()-10);
				if(list2.size()<1){
					lv.setVisibility(View.GONE);
					img_none.setVisibility(View.VISIBLE);
				}else{
					lv.setVisibility(View.VISIBLE);
					img_none.setVisibility(View.GONE);
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
	
			
			volleryUtil.execute(Back_money_new.this, Constant.URL+"api/v6/invest/repayDetailList/"+investId,"" );
		
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1; 
				list2.clear();
				initData();
			}else{
				
				
				
			}
			Toast.makeText(Back_money_new.this, "刷新成功", 2000).show();
			lv.onRefreshComplete();
		};
	};
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}
	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.rl_back:
			finish();
			break;
		}
	}

}
